import {Component, inject, OnInit} from '@angular/core';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Subscription } from 'rxjs';
import { AuthService } from '../services/auth.service';
import { User } from '../models/auth.model';

@Component({
  selector: 'app-dashboard',
  standalone: false,
  templateUrl: './dashboard.component.html',
  styleUrl: './dashboard.component.less',
})
export class DashboardComponent implements OnInit {
  private authService = inject(AuthService);
  private router = inject(Router);
  private message = inject(NzMessageService);
  private subscription: Subscription = new Subscription();

  currentUser: User | null = null;

  ngOnInit(): void {
    // 检查是否已登录
    if (!this.authService.isLoggedIn()) {
      this.router.navigate(['/login']);
      return;
    }

    // 订阅当前用户状态
    this.subscription.add(
      this.authService.currentUser$.subscribe(user => {
        console.log('user', user);
        this.currentUser = user;
      })
    );
  }
  // 登出
  logout(): void {
    this.authService.logout();
    this.message.success('已成功退出登录');
    this.router.navigate(['/login']);
  }

  // 获取用户角色文本
  getUserRoleText(role: number): string {
    switch (role) {
      case 1:
        return '超级管理员';
      case 2:
        return '管理员';
      case 3:
        return '普通用户';
      default:
        return '未知';
    }
  }
}
